package minio.controller;

import io.minio.ObjectWriteResponse;
import jackson.util.BeanUtil;
import lombok.RequiredArgsConstructor;
import minio.client.MinioWebTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequiredArgsConstructor
@RequestMapping("multipart")
public class FileController {
    final MinioWebTemplate minioWebTemplate;

    @PostMapping("file")
    public Object testGetPojo(MultipartFile file) {
        return minioWebTemplate.upload(file);
    }

    @CrossOrigin
    @GetMapping("create")
    public Object create(String fileName, Integer partTotal) {
        return minioWebTemplate.createMultipartUpload(fileName, partTotal);
    }

    @CrossOrigin
    @GetMapping("complete")
    public Object complete(String fileName, String uploadId) {
        ObjectWriteResponse objectWriteResponse = minioWebTemplate.completeMultipartUpload(fileName, uploadId);
        return BeanUtil.beanToMap(objectWriteResponse);
    }
}